<?php
class lotto6h
{

    public $g_betname;
    public $g_bettype_ab;
    public $m_lotto_id = 0;
    public $autoport = 0;
    public $betmin = 5;
    public $on3 = -1;
    public $on2 = -1;
    public $on1 = -1;
    public $op3sd;
    public $op3bs;
    public $op2sd;
    public $op2bs;
    public $op1sd;
    public $op1bs;
    public $op3pc;
    public $op2pc;
    public $op1pc;
    public $or2p0sd;
    public $or2p1sd;
    public $or2p2sd;
    public $op321sd;
    public $op321bs;
    public $or2;
    public $op3;
    public $or3;
    public $oamount = 0;
    public $omb = 0;
    public $oad = 0;
    public $oua = 0;
    public $our = 0;
    public $ogg = 0;
    public $bettotal_by_lotto_id = true;
    public $bettotal_6h = "";
    public $tbl;

    public function lotto6h ()
    {
        global $g_betname;
        global $g_bettype_ab;
        $this->g_betname = $g_betname;
        $this->g_bettype_ab = $g_bettype_ab;
    }

    public function GetConfig ($id)
    {
        $val = 0;
        $rs = getdb("Select * From k_config Where id = '{$id}'");
        $val = $rs->f['value'];
        return $val;
    }

    public function GetUserID ($user_id)
    {
        $ret = array();
        $sql = "select A.id as id_0, B.id as id_1,C.id as id_2,D.id as id_3,E.id as id_4, 0 as id_5 from k_user A";
        $sql .= " left join k_user B ON B.id = A.prev_id";
        $sql .= " left join k_user C ON C.id = B.prev_id";
        $sql .= " left join k_user D ON D.id = C.prev_id";
        $sql .= " left join k_user E ON E.id = D.prev_id";
        $sql .= " where E.id is not NULL";
        $sql .= " and A.id = " . $user_id;
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $i = 1;
            while ($i <= 5)
            {
                if (empty($rs->f["id_" . $i]))
                {
                    array_push($ret, 0);
                }
                else
                {
                    array_push($ret, $rs->f["id_" . $i]);
                }
                ++$i;
            }
        }
        return $ret;
    }

    public function GetAgentID ($user_id)
    {
        $ret = array();
        $sql = "select A.id as id_0, B.id as id_1,C.id as id_2,D.id as id_3,E.id as id_4, 0 as id_5 from k_user A";
        $sql .= " left join k_user B ON B.id = A.prev_id";
        $sql .= " left join k_user C ON C.id = B.prev_id";
        $sql .= " left join k_user D ON D.id = C.prev_id";
        $sql .= " left join k_user E ON E.id = D.prev_id";
        $sql .= " Where A.id =" . $user_id;
        $rs = getdb($sql);
        if (!$rs->eof)
        {
            $i = 1;
            while ($i <= 5)
            {
                if (empty($rs->f["id_" . $i]))
                {
                    array_push($ret, $rs->f["id_" . $i]);
                }
                ++$i;
            }
        }
        return $ret;
    }

    public function GetAgentUserIDScript ($user_id, $acl)
    {
        $sl = "select A.id from k_user A";
        $sl .= " left join k_user B ON B.id = A.prev_id";
        $sl .= " left join k_user C ON C.id = B.prev_id";
        $sl .= " left join k_user D ON D.id = C.prev_id";
        $sl .= " left join k_user E ON E.id = D.prev_id";
        switch ($acl)
        {
            case 2 :
                $sl .= " Where B.id IS NOT NULL and B.id = " . $user_id;
                break;
            case 3 :
                $sl .= " Where C.id IS NOT NULL and C.id = " . $user_id;
                break;
            case 4 :
                $sl .= " Where D.id IS NOT NULL and D.id = " . $user_id;
                break;
            case 5 :
                $sl .= " Where E.id IS NOT NULL and E.id = " . $user_id;
                break;
        }
        $rs = getdb($sl);
        $tmpArr = array();
        while (!$rs->eof)
        {
            array_push($tmpArr, $rs->f[0]);
            $rs->next();
        }
        $cmdStr = "(" . implode(",", $tmpArr) . ")";
        return $cmdStr;
    }

    public function GetUserAmounts ($user_id, $bettype, $tagStr)
    {
        $amt = 0;
        $rs = getdb("Select A.* From amounts A Left Join k_user_percent B ON B.user_id = A.user_id Where A.user_id=" . $user_id . " and A.bettype=" . $bettype . " AND IFNULL(B.percent_sw , 0)=0 AND B.gametype = 0");
        if (!$rs->eof)
        {
            $amt = $rs->f[$tagStr];
        }
        return $amt;
    }

    public function GetBetTotalFromAgentId ($acl, $u_id, $rs)
    {
        $amt = 0;
        $fn = "";
        $s = $this->GetAgentUserIDScript($u_id, $acl);
        switch ($acl)
        {
            case 2 :
                $fn_bet = "mpercent";
                break;
            case 3 :
                $fn_bet = "percent2";
                $fn_bet2 = "percent3";
                break;
            case 4 :
                $fn_bet = "percent3";
                $fn_bet2 = "percent4";
                break;
            case 5 :
                $fn_bet = "percent4";
                $fn_bet2 = "percent5";
                break;
            case 6 :
            case 255 :
                $fn_bet = "upercent";
                $fn_bet2 = "percent255";
                break;
        }
        $sql = "Select SUM(amount*" . $fn_bet . "/100" . ") as amount From bet Where lotto_id=" . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and numbers='" . $rs->f['numbers'] . "' and user_id in " . $s;
        $q = getdb($sql);
        $amt = isset($q->f['amount']) ? $q->f['amount'] : "0";
        if (3 <= $acl)
        {
            $sql = "Select SUM(amount*" . $fn_bet2 . "/100" . ") as amount From bet2 Where lotto_id=" . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and numbers='" . $rs->f['numbers'] . "' and uperid" . $acl . "=" . $u_id;
            $q = getdb($sql);
            $amt += isset($q->f['amount']) ? $q->f['amount'] : "0";
        }
        if ($acl <= 5)
        {
            $sql = "Select SUM( amount " . ( ") as amount From bet2 Where lotto_id=" ) . $this->m_lotto_id . " and bettype=" . $rs->f['bettype'] . " and numbers='" . $rs->f['numbers'] . "' and user_id=" . $u_id;
            $q = getdb($sql);
            $q->f['amount'] = isset($q->f['amount']) ? $q->f['amount'] : "0";
            $amt -= $q->f['amount'];
        }
        return $amt;
    }

    public function getBet_6h ($id = false)
    {
        if ($id === false)
        {
            
        }
        else
        {
            $cmdStr = "";
            $this->autoport = false;
            if ($this->GetConfig("protect_start") == "1" && $this->GetConfig("autoport_mod") == "1")
            {
                $this->autoport = 1;
            }
            $this->betmin = floatval($this->GetConfig("betmin"));
            if (0 < $id)
            {
                $rs = getdb("!!Select * From bet Where IFNULL(state,0)=0 and id={$id} and (rame=0 or rame=2)");
            }
            else
            {
                $rs = getdb("!!Select * From bet Where IFNULL(state,0)=0 and (rame=0 or rame=2)");
            }
            $this->m_lotto_id = 0;
            while (!$rs->eof)
            {
                $this->m_lotto_id = $rs->f['lotto_id'];
                if ($this->bettotal_by_lotto_id)
                {
                    $this->bettotal_6h = "bettotal_6h_{$this->m_lotto_id}";
                }
                else
                {
                    $this->bettotal_6h = "bettotal_6h_0";
                }
                if (empty($this->g_bettype_ab[intval($rs->f['bettype'])][0]))
                {
                    $this->SetBetTotal($rs, $rs->f['bettype'], $rs->f['bettype']);
                }
                else
                {
                    $old = $this->autoport;
                    $this->autoport = false;
                    $this->SetBetTotal($rs, $rs->f['bettype'], $rs->f['bettype']);
                    $this->autoport = $old;
                    $this->SetBetTotal($rs, $this->g_bettype_ab[intval($rs->f['bettype'])][0], $this->g_bettype_ab[intval($rs->f['bettype'])][1]);
                }
                $cmdStr = "Update bet Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1";
                getdb($cmdStr);
                $rs->next();
            }
        }
    }

    public function SetBetTotal ($sqlQ, $btype1, $btype2)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $user_id = $sqlQ->f['user_id'];
        $disk = $sqlQ->f['disk'];
        $bettype = $sqlQ->f['bettype'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $percent2 = $sqlQ->f['mpercent'];
        $percent3 = $sqlQ->f['percent2'];
        $percent4 = $sqlQ->f['percent3'];
        $percent5 = $sqlQ->f['percent4'];
        $percent255 = $sqlQ->f['upercent'];
        $back1 = $sqlQ->f['back1'];
        $back2 = $sqlQ->f['back2'];
        $back3 = $sqlQ->f['back3'];
        $back4 = $sqlQ->f['back4'];
        $back5 = $sqlQ->f['back5'];
        $back6 = $sqlQ->f['back6'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $amount = $sqlQ->f['amount'] * $rame;
        $amount2 = 0;
        $amount3 = 0;
        $amount4 = 0;
        $damount2 = 0;
        $fback1 = 0;
        $fback2 = 0;
        $fback3 = 0;
        $is2526 = false;
        $iscombination = false;
        $rec = 1;
        $nums = array();
        if ($btype1 == 25 || $btype1 == 26 || $btype1 == 1003)
        {
            $is2526 = true;
            $p = explode(" ", $numbers);
            array_push($nums, $p[0] . "," . $p[1]);
        }
        else if ($btype1 == 19)
        {
            $p = explode("|", $numbers);
            array_push($nums, $p[0]);
            $iscombination = true;
        }
        else
        {
            array_push($nums, $numbers);
        }
        $cmdStr = "";
        $lamt = 0;
        $acl = "";
        $users = $this->GetUserID($user_id);
        $famt = 0;
        $famt_acl = array(0, 0, 0, 0, 0);
        $percent_ = array(0, 0, 0, 0, 0, 0);
        if ($this->autoport)
        {
            $rs6h = getdb("Select A.protmod from k_user_percent A Left Join k_user B ON B.id = A.user_id where A.user_id=" . $users[3] . " and B.acl=5 and A.gametype = 0");
            if ($rs6h->f['protmod'] == 0)
            {
                $this->autoport = false;
            }
        }
        $percent_sw = 0;
        if ($this->autoport)
        {
            $i = 0;
            while ($i < count($users) - 1)
            {
                $usr = get_user(intval($users[$i]));
                $percent_[$i + 1] = intval($usr['percent2']);
                ++$i;
            }
        }
        $bet2idArr = array();
        $i = 0;
        while ($i < count($users))
        {
            $u_id = intval($users[$i]);
            $famt = 0;
            $rate_s = 0;
            switch ($i)
            {
                case 0 :
                    $rate_s = $sqlQ->f['rate1'] + $sqlQ->f['rate5'];
                    break;
                case 1 :
                    $rate_s = $sqlQ->f['rate1'] + $sqlQ->f['rate2'] + $sqlQ->f['rate5'];
                    break;
                case 2 :
                    $rate_s = $sqlQ->f['rate1'] + $sqlQ->f['rate2'] + $sqlQ->f['rate3'] + $sqlQ->f['rate5'];
                    break;
                case 3 :
                    $rate_s = $sqlQ->f['rate1'] + $sqlQ->f['rate2'] + $sqlQ->f['rate3'] + $sqlQ->f['rate4'] + $sqlQ->f['rate5'];
                    break;
                case 4 :
                    $rate_s = $sqlQ->f['rate1'] + $sqlQ->f['rate2'] + $sqlQ->f['rate3'] + $sqlQ->f['rate4'] + $sqlQ->f['rate5'];
                    break;
            }
            $rate = $sqlQ->f['rate'] + $rate_s;
            $amount1 = array_sum($famt_acl) + $amount;
            $amount3 = round($amount * $rate, 3);
            $fback1 = round($amount * ( 100 - $back5 ) / 100, 3);
            switch ($i)
            {
                case 0 :
                    $amount2 = round($amount * $percent2 / 100, 3);
                    $damount2 = round($amount * $percent2 / 100, 3);
                    $amount4 = round($amount * $rate * $percent2 / 100, 3);
                    $fback2 = round(( $back1 - $back2 ) * $amount / 100, 3);
                    $fback3 = round($fback1 * $percent2 / 100, 3);
                    break;
                case 1 :
                    $amount2 = round($famt_acl[0] * $percent_[1] / 100 + $amount * $percent3 / 100, 3);
                    $damount2 = round($amount * $percent3 / 100, 3);
                    $amount4 = round($amount * $rate * $percent3 / 100, 3);
                    $fback2 = round(( $back2 - $back3 ) * $amount / 100, 3);
                    $fback2 -= $fback2 * $percent2 / 100;
                    $fback3 = round($fback1 * $percent3 / 100, 3);
                    break;
                case 2 :
                    $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) / 100 + $famt_acl[1] * $percent_[2] / 100 + $amount * $percent4 / 100, 3);
                    $damount2 = round($amount * $percent4 / 100, 3);
                    $amount4 = round($amount * $rate * $percent4 / 100, 3);
                    $fback2 = round(( $back3 - $back4 ) * $amount / 100, 3);
                    $fback2 -= $fback2 * ( $percent3 + $percent2 ) / 100;
                    $fback3 = round($fback1 * $percent4 / 100, 3);
                    break;
                case 3 :
                    $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) / 100 + $famt_acl[1] * round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) / 100 + $famt_acl[2] * $percent_[3] / 100 + $amount * $percent5 / 100, 3);
                    $damount2 = round($amount * $percent5 / 100, 3);
                    $amount4 = round($amount * $rate * $percent5 / 100, 3);
                    $fback2 = round(( $back4 - $back5 ) * $amount / 100, 3);
                    $fback2 -= $fback2 * ( $percent4 + $percent3 + $percent2 ) / 100;
                    $fback3 = round($fback1 * $percent5 / 100, 3);
                    break;
                case 4 :
                    $amount2 = round($famt_acl[0] * ( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) - round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[1] * ( 100 - $percent_[2] - round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[2] * ( 100 - $percent_[3] ) / 100 + $famt_acl[3] + $amount * $percent255 / 100, 3);
                    $damount2 = round($amount * $percent255 / 100, 3);
                    $amount4 = round($amount * $rate * $percent255 / 100, 3);
                    $fback2 = 0;
                    $fback3 = round($fback1 * $percent255 / 100, 3);
                    break;
            }
            $acl = $i + 2;
            $lamt = $amount2;
            if ($this->autoport && $rame == 1)
            {
                $auto_nt = $this->GetUserAmounts($u_id, $btype2, "auto_nt");
            }
            else
            {
                $auto_nt = 0;
            }
            if (!$iscombination)
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[0]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $acl < 6)
                    {
                        if ($auto_nt + $this->betmin <= $lamt)
                        {
                            $famt = intval($lamt - $auto_nt);
                            $lamt -= $famt;
                            $amount1 -= $famt;
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id,user_id,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values (" . strval($lotto_id) . "," . strval($u_id) . "," . strval($btype1) . "," . "'" . $nums[0] . "'," . $rec . "," . strval($amount1) . "," . strval($lamt) . "," . strval($amount3) . "," . strval($amount4) . "," . strval($fback1) . "," . strval($fback2) . "," . strval($fback3) . "," . floatval($rate) . "," . floatval($rate);
                    $k = 1;
                    while ($k <= 4)
                    {
                        if ($disk == $k)
                        {
                            $cmdStr .= ",1," . strval($amount) . "," . strval($damount2) . "," . strval($amount3) . "," . strval($amount4) . "," . strval($fback1) . "," . strval($fback2) . "," . strval($fback3) . "," . floatval($rate) . "," . floatval($rate);
                        }
                        else
                        {
                            $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        }
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                }
                else
                {
                    if ($this->autoport && 0 < $auto_nt && $acl < 6)
                    {
                        if ($auto_nt + $this->betmin <= $sqlBetTotal->f['amount2'] + $lamt)
                        {
                            $famt = intval($sqlBetTotal->f['amount2'] + $lamt - $auto_nt);
                            $lamt -= $famt;
                            $amount1 -= $famt;
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $cmdStr = "Update {$this->bettotal_6h} Set " . "totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . strval($amount1) . ",amount2 = amount2 +" . strval($lamt) . ",amount3 = amount3 +" . strval($amount3) . ",amount4 = amount4 +" . strval($amount4) . ",back1 = back1 +" . strval($fback1) . ",back2 = back2 +" . strval($fback2) . ",back3 = back3 +" . strval($fback3) . ",rate1 = ((totalnum-1)*rate1 +" . floatval($rate) . ")/(totalnum)" . ",rate2 = amount3 / amount1";
                    $cdisk = substr("abcd", $disk - 1, 1);
                    $cmdStr .= ",totalnum" . $cdisk . " = totalnum" . $cdisk . " + " . strval($rame) . ",amount1" . $cdisk . " = amount1" . $cdisk . " +" . strval($amount) . ",amount2" . $cdisk . " = amount2" . $cdisk . " +" . strval($damount2) . ",amount3" . $cdisk . " = amount3" . $cdisk . " +" . strval($amount3) . ",amount4" . $cdisk . " = amount4" . $cdisk . " +" . strval($amount4) . ",back1" . $cdisk . " = back1" . $cdisk . " +" . strval($fback1) . ",back2" . $cdisk . " = back2" . $cdisk . " +" . strval($fback2) . ",back3" . $cdisk . " = back3" . $cdisk . " +" . strval($fback3) . ",rate1" . $cdisk . " = ((totalnum" . $cdisk . "-1)*rate1" . $cdisk . " +" . floatval($rate) . ")/(totalnum" . $cdisk . ")" . ",rate2" . $cdisk . " = amount3" . $cdisk . " / amount1" . $cdisk;
                    $cmdStr .= ",updatetime =" . systime() . " Where lotto_id=" . intval($lotto_id) . " and user_id=" . intval($u_id) . " and bettype=" . intval($btype1) . " and numbers='" . $nums[0] . "' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            }
            else
            {
                $gamt = 0;
                if ($this->autoport && 0 < $auto_nt && $acl < 6)
                {
                    $gamt = $this->GetBetTotalFromAgentId($i + 2, $u_id, $sqlQ);
                    if ($auto_nt + $this->betmin <= $gamt)
                    {
                        $famt = $gamt - $auto_nt;
                        $amount1 -= $famt;
                    }
                    if (0 < $famt)
                    {
                        ++$rec;
                    }
                }
                $j = 0;
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[$j]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin < $gamt && $acl < 6)
                    {
                        $lamt = $auto_nt;
                    }
                    $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id,user_id,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1 ,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values ({$lotto_id},{$u_id},{$btype1},'{$nums[$j]}',{$rec},{$amount1},{$lamt}," . "{$amount3},{$amount4},{$fback1},{$fback2},{$fback3},{$rate},{$rate}";
                    $k = 1;
                    while ($k <= 4)
                    {
                        if ($disk == $k)
                        {
                            $cmdStr .= ",1,{$amount},{$damount2}," . "{$amount3},{$amount4},{$fback1},{$fback2},{$fback3},{$rate},{$rate}";
                        }
                        else
                        {
                            $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        }
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                }
                else
                {
                    if ($this->autoport && 0 < $auto_nt && $acl < 6 && $auto_nt + $this->betmin < $gamt)
                    {
                        $lamt = $auto_nt - ( $gamt - $amount2 );
                    }
                    $cmdStr = "Update {$this->bettotal_6h} Set " . "totalnum = totalnum +" . strval($rec * $rame) . ",amount1 = amount1 +" . $amount1 . ",amount2 = amount2 +" . $lamt . ",amount3 = amount3 +" . $amount3 . ",amount4 = amount4 +" . $amount4 . ",back1 = back1 +" . $fback1 . ",back2 = back2 +" . $fback2 . ",back3 = back3 +" . $fback3 . ",rate1 = ((totalnum-1)*rate1 +" . $rate . ")/(totalnum)" . ",rate2 = amount3 / amount1";
                    $cdisk = substr("abcd", $disk - 1, 1);
                    $cmdStr .= ",totalnum" . $cdisk . " = totalnum" . $cdisk . " +" . $rame . ",amount1" . $cdisk . " = amount1" . $cdisk . " +" . $amount . ",amount2" . $cdisk . " = amount2" . $cdisk . " +" . $damount2 . ",amount3" . $cdisk . " = amount3" . $cdisk . " +" . $amount3 . ",amount4" . $cdisk . " = amount4" . $cdisk . " +" . $amount4 . ",back1" . $cdisk . " = back1" . $cdisk . " +" . $fback1 . ",back2" . $cdisk . " = back2" . $cdisk . " +" . $fback2 . ",back3" . $cdisk . " = back3" . $cdisk . " +" . $fback3 . ",rate1" . $cdisk . " = ((totalnum" . $cdisk . "-1)*rate1" . $cdisk . " +" . $rate . ")/(totalnum" . $cdisk . ")" . ",rate2" . $cdisk . " = amount3" . $cdisk . " / amount1" . $cdisk;
                    $cmdStr .= ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=" . $u_id . " and bettype=" . $btype1 . " and numbers='" . $nums[$j] . "' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            }
            if ($this->autoport && 0 < $auto_nt && 0 < $famt && $acl < 6)
            {
                $Bet2 = $this->InsertBet2_6h($this->m_lotto_id, $users, $acl, $famt, $sqlQ, $nums);
                $famt_acl[$i] = $famt;
                if ($Bet2 != null)
                {
                    $bet2idArr[] = $Bet2['id'];
                }
            }
            ++$i;
        }
        if (0 < count($bet2idArr))
        {
            foreach ($bet2idArr as $id)
            {
                $this->getBet2_6h($id, 0);
            }
        }
    }

    public function InsertBet2_6h ($lotto_id, $users, $acl, $amount, $sqlBet, $nums)
    {
        $btype = $sqlBet->f['bettype'];
        $mod = 1;
        $betdoc = "";
        $rsfile = "";
        $num = $nums[0];
        $ratefd = $num;
        $type = 0;
        switch ($btype)
        {
            case 0 :
            case 24 :
                $rsfile = 0;
                break;
            case 1 :
            case 2 :
            case 3 :
            case 13 :
            case 14 :
            case 18 :
            case 20 :
            case 28 :
            case 30 :
            case 34 :
            case 35 :
            case 46 :
            case 47 :
                $rsfile = 1;
                $ratefd = $btype . "_" . $num;
                break;
            case 4 :
            case 31 :
                $rsfile = 4;
                break;
            case 5 :
            case 6 :
                $rsfile = 5;
                $ratefd = $btype . "_" . $num;
                break;
            case 21 :
            case 37 :
                $rsfile = 1;
                $ratefd = "21_" . $num;
                break;
            case 22 :
            case 38 :
                $rsfile = 1;
                $ratefd = "22_" . $num;
                break;
            case 7 :
            case 8 :
            case 9 :
            case 10 :
            case 11 :
            case 29 :
            case 39 :
            case 40 :
            case 41 :
            case 42 :
            case 43 :
            case 44 :
            case 45 :
                $mod = 3;
                break;
            case 19 :
                $rsfile = 1;
                $type = substr($sqlBet->f['numbers'], 0, 4);
                $ratefd = $btype . "_" . $type;
                $num = substr($sqlBet->f['numbers'], 5);
                $mod = 2;
                break;
            case 15 :
            case 16 :
            case 17 :
            case 23 :
                $rsfile = 15;
                $type = substr($sqlBet->f['numbers'], 0, 1);
                $num = substr($sqlBet->f['numbers'], 2, 2);
                $ratefd = "15_" . $type . "_" . $num;
                $mod = 2;
                break;
            case 25 :
            case 26 :
                $type = substr($sqlBet->f['numbers'], 0, 1);
                $rsfile = "4" . $type;
                $num = substr($sqlBet->f['numbers'], 2, 2);
                $ratefd = d2($num);
                $mod = 2;
                break;
            case 27 :
                $rsfile = 27;
                $ratefd = $rate;
                $mod = 2;
                break;
            default :
                return null;
                break;
        }
        $uid = $users[$acl - 2];
        $pid = $users[$acl - 2 + 1];
        $u2 = get_user($users[0]);
        $u3 = get_user($users[1]);
        $u4 = get_user($users[2]);
        $u5 = get_user($users[3]);
        $stime = systime();
        $retBet2 = null;
        switch ($mod)
        {
            case 1 :
                $retBet2 = bet2in1($uid, $acl, $pid, $btype, $num, $amount, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime, $lotto_id);
                break;
            case 2 :
                $retBet2 = bet2in2($uid, $acl, $pid, $btype, $type, $num, $amount, $rsfile, $ratefd, $u2, $u3, $u4, $u5, $stime, $lotto_id);
                break;
            case 3 :
                return null;
                break;
        }
        return $retBet2;
    }

    public function getBet2_6h ($id = false, $mode = 1)
    {
        if ($id === false)
        {
            return FALSE;
        }
        else
        {
            $cmdStr = "";
            $this->autoport = false;
            if ($this->GetConfig("protect_start") !== "0" && $this->GetConfig("autoport_mod") !== "0" && 0 < $mode)
            {
                $this->autoport = true;
            }
            $this->betmin = $this->GetConfig("betmin");
            if (0 < $id)
            {
                $sqlBet2 = getdb("!!Select * From bet2 Where id = {$id} AND IFNULL(state,0)=0 and (rame=0 or rame=2)");
            }
            else
            {
                $sqlBet2 = getdb("!!Select * From bet2 Where IFNULL(state,0)=0 and (rame=0 or rame=2)");
            }
            $this->m_lotto_id = 0;
            while (!$sqlBet2->eof)
            {
                $this->m_lotto_id = $sqlBet2->f['lotto_id'];
                if ($this->bettotal_by_lotto_id)
                {
                    $this->bettotal_6h = "bettotal_6h_" . $this->m_lotto_id;
                }
                else
                {
                    $this->bettotal_6h = "bettotal_6h";
                }
                if (empty($this->g_bettype_ab[intval($sqlBet2->f['bettype'])][0]))
                {
                    $this->SetBet2Total($sqlBet2, $sqlBet2->f['bettype'], $sqlBet2->f['bettype'], $mode);
                }
                else
                {
                    $this->SetBet2Total($sqlBet2, $this->g_bettype_ab[intval($sqlBet2->f['bettype'])][0], $this->g_bettype_ab[intval($sqlBet2->f['bettype'])][1], $mode);
                }
                $cmdStr = "Update bet2 Set state=1 Where id=" . $sqlBet2->f['id'] . " LIMIT 1";
                getdb($cmdStr);
                $sqlBet2->next();
            }
        }
    }

    public function SetBet2Total ($sqlQ, $btype1, $btype2, $mode = 1)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $user_id = $sqlQ->f['user_id'];
        $acl = $sqlQ->f['acl'];
        $bettype = $sqlQ->f['bettype'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $percent3 = $sqlQ->f['percent3'];
        $percent4 = $sqlQ->f['percent4'];
        $percent5 = $sqlQ->f['percent5'];
        $percent255 = $sqlQ->f['percent255'];
        $back2 = $sqlQ->f['back2'];
        $back3 = $sqlQ->f['back3'];
        $back4 = $sqlQ->f['back4'];
        $back5 = $sqlQ->f['back5'];
        $back6 = $sqlQ->f['back6'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $amount = $sqlQ->f['amount'] * $rame;
        $is2526 = false;
        $iscombination = false;
        $rec = 1;
        $isowner = 1;
        $nums = array();
        $nums = array();
        if ($btype1 == 25 || $btype1 == 26 || $btype1 == 1003)
        {
            $is2526 = true;
            $p = explode(" ", $numbers);
            array_push($nums, $p[0] . "," . $p[1]);
        }
        else if ($btype1 == 19)
        {
            $p = explode("|", $numbers);
            $iscombination = true;
            array_push($nums, $p[0]);
        }
        else
        {
            array_push($nums, $numbers);
        }
        $cmdStr = "";
        $amount2 = 0;
        $amount1 = 0;
        $amount4 = 0;
        $fback1 = 0;
        $fback2 = 0;
        $fback3 = 0;
        $lamt = 0;
        $famt = 0;
        $pacl = "";
        $famt_acl = array(0, 0, 0, 0, 0);
        $percent_ = array(0, 0, 0, 0, 0, 0);
        $agents = array();
        $i = 2;
        while ($i <= 6)
        {
            if ($i < $acl)
            {
                array_push($agents, 0);
            }
            else if ($i == $acl)
            {
                array_push($agents, $user_id);
            }
            else if ($i == 6)
            {
                array_push($agents, 0);
            }
            else
            {
                array_push($agents, $sqlQ->f["uperid" . $i]);
            }
            ++$i;
        }
        $percent_sw = 0;
        if ($this->autoport)
        {
            $i = 0;
            while ($i < count($agents) - 1)
            {
                $usr = get_user(intval($agents[$i]));
                $percent_[$i + 1] = intval($usr['percent2']);
                ++$i;
            }
        }
        $bet2idArr = array();
        for ($i = 0; $i < count($agents); $i++)
        {
            $famt = 0;
            $u_id = $agents[$i];
            if ($i + 2 < $acl)
            {
                continue;
            }
            if ($i + 2 == $acl)
            {
                $isowner = 0 - 1;
                $amount1 = $amount * $mode;
                $amount2 = $amount;
                $amount3 = round($amount * $rate, 0);
                $amount4 = round($amount * $rate, 0);
                $fback1 = round($amount * ( 100 - $back5 ) / 100, 3);
                $fback2 = $fback1;
                $fback3 = 0;
            }
            else
            {
                $isowner = 1;
                $amount1 = array_sum($famt_acl) + $amount * $mode;
                $amount3 = round($amount * $rate, 0);
                $fback1 = round($amount * ( 100 - $back5 ) / 100, 3);
                switch ($i)
                {
                    case 1 :
                        $amount2 = round($famt_acl[0] * $percent_[1] / 100 + $amount * $percent3 / 100, 3);
                        $amount4 = round($amount * $rate * $percent3 / 100, 0);
                        $fback2 = round(( $back2 - $back3 ) * $amount / 100, 3);
                        $fback3 = round($fback1 * $percent3 / 100, 3);
                        break;
                    case 2 :
                        $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) / 100 + $famt_acl[1] * $percent_[2] / 100 + $amount * $percent4 / 100, 3);
                        $amount4 = round($amount * $rate * $percent4 / 100, 0);
                        $fback2 = round(( $back3 - $back4 ) * $amount / 100, 3);
                        if ($acl == 2)
                        {
                            $fback2 -= $fback2 * $percent3 / 100;
                        }
                        $fback3 = round($fback1 * $percent4 / 100, 3);
                        break;
                    case 3 :
                        $amount2 = round($famt_acl[0] * round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) / 100 + $famt_acl[1] * round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) / 100 + $famt_acl[2] * $percent_[3] / 100 + $amount * $percent5 / 100, 3);
                        $amount4 = round($amount * $rate * $percent5 / 100, 0);
                        $fback2 = round(( $back4 - $back5 ) * $amount / 100, 3);
                        if ($acl == 2)
                        {
                            $fback2 -= $fback2 * ( $percent4 + $percent3 ) / 100;
                        }
                        else if ($acl == 3)
                        {
                            $fback2 -= $fback2 * $percent4 / 100;
                        }
                        $fback3 = round($fback1 * $percent5 / 100, 3);
                        break;
                    case 4 :
                        $amount2 = round($famt_acl[0] * ( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) - round(( 100 - $percent_[1] - round(( 100 - $percent_[1] ) * $percent_[2] / 100, 0) ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[1] * ( 100 - $percent_[2] - round(( 100 - $percent_[2] ) * $percent_[3] / 100, 0) ) / 100 + $famt_acl[2] * ( 100 - $percent_[3] ) / 100 + $famt_acl[3] + $amount * $percent255 / 100, 3);
                        $amount4 = round($amount * $rate * $percent255 / 100, 0);
                        $fback2 = 0;
                        $fback3 = round($fback1 * $percent255 / 100, 3);
                        break;
                }
            }
            $pacl = $i + 2;
            $lamt = $amount2 * $mode;
            if ($this->autoport && 0 < $lamt && ( $acl < $pacl && $pacl < 6 ) && $rame == 1)
            {
                $auto_nt = $this->GetUserAmounts($u_id, $btype2, "auto_nt");
            }
            else
            {
                $auto_nt = 0;
            }
            if (!$iscombination)
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[0]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin <= $sqlBetTotal->f['amount2'] + $lamt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        $lamt = $auto_nt;
                    }
                    $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id,user_id,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values (" . $lotto_id . "," . $u_id . "," . $btype1 . "," . "'" . $nums[0] . "'," . $rec . "," . $amount1 * $isowner . "," . $lamt * $isowner . "," . $amount3 * $isowner . "," . $amount4 * $isowner . "," . $fback1 . "," . $fback2 . "," . $fback3 . "," . $rate . "," . $rate;
                    $k = 1;
                    while ($k <= 4)
                    {
                        $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                }
                else
                {
                    if ($this->autoport && 0 < $auto_nt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        if ($auto_nt + $this->betmin <= $sqlBetTotal->f['amount2'] + $lamt)
                        {
                            $famt = intval($sqlBetTotal->f['amount2'] + $lamt - $auto_nt);
                            $lamt -= $famt;
                        }
                        if (0 < $famt)
                        {
                            ++$rec;
                        }
                    }
                    $cmdStr = "Update {$this->bettotal_6h} Set totalnum = totalnum +" . strval($rec * $rame * $mode) . ",amount1 = amount1 +" . $amount1 * $isowner . ",amount2 = amount2 +" . $lamt * $isowner . ",amount3 = amount3 +" . $amount3 * $isowner . ",amount4 = amount4 +" . $amount4 * $isowner . ",back1 = back1 +{$fback1}" . ",back2 = back2 +{$fback2}" . ",back3 = back3 +{$fback3}" . ",rate1 = ((totalnum-1)*rate1 +{$rate} )/(totalnum)" . ",rate2 = amount3 / amount1" . ",updatetime =" . systime() . " Where lotto_id={$lotto_id}" . " and user_id={$u_id}" . " and bettype={$btype1}" . " and numbers='{$nums[0]}' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            }
            else
            {
                $gamt = 0;
                if ($this->autoport && 0 < $auto_nt && ( $acl < $pacl && $pacl < 6 ))
                {
                    $gamt = $this->GetBetTotalFromAgentId($i + 2, $u_id, $sqlQ);
                    if ($auto_nt + $this->betmin <= $gamt)
                    {
                        $famt = $gamt - $auto_nt;
                    }
                    if (0 < $famt)
                    {
                        ++$rec;
                    }
                }
                $j = 0;
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[$j]}'");
                if ($sqlBetTotal->eof)
                {
                    if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin <= $gamt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        $lamt = $auto_nt;
                    }
                    $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id,user_id,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3 ,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values (" . $lotto_id . "," . $u_id . "," . $btype1 . "," . "'" . $nums[$j] . "'," . $rec . "," . $amount1 * $isowner . "," . $lamt * $isowner . "," . $amount3 * $isowner . "," . $amount4 * $isowner . "," . $fback1 . "," . $fback2 . "," . $fback3 . "," . $rate . "," . $rate;
                    $k = 1;
                    while ($k <= 4)
                    {
                        $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                }
                else
                {
                    if ($this->autoport && 0 < $auto_nt && $auto_nt + $this->betmin <= $gamt && ( $acl < $pacl && $pacl < 6 ))
                    {
                        $lamt = $auto_nt - ( $gamt - $amount2 );
                    }
                    $cmdStr = "Update {$this->bettotal_6h} Set " . "totalnum = totalnum +" . strval($rec * $rame * $mode) . ",amount1 = amount1 +" . $amount1 * $isowner . ",amount2 = amount2 +" . $lamt * $isowner . ",amount3 = amount3 +" . $amount3 * $isowner . ",amount4 = amount4 +" . $amount4 * $isowner . ",back1 = back1 +" . $fback1 . ",back2 = back2 +" . $fback2 . ",back3 = back3 +" . $fback3 . ",rate1 = ((totalnum-1)*rate1 +" . $rate . ")/(totalnum)" . ",rate2 = amount3 / amount1" . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=" . $u_id . " and bettype=" . $btype1 . " and numbers='" . $nums[$j] . "' LIMIT 1";
                }
                if (!empty($cmdStr))
                {
                    getdb($cmdStr);
                }
            }
            if ($this->autoport && 0 < $auto_nt && 0 < $famt && ( $acl < $pacl && $pacl < 6 ))
            {
                $Bet2 = $this->InsertBet2_6h($this->m_lotto_id, $agents, $pacl, $famt, $sqlQ, $nums);
                $famt_acl[$i] = $famt;
                if ($Bet2 != null)
                {
                    $bet2idArr[] = $Bet2['id'];
                }
            }
        }
        if (0 < count($bet2idArr))
        {
            foreach ($bet2idArr as $id)
            {
                $this->getBet2_6h($id, 0);
            }
        }
    }

    public function getBet8_6h ($id = false)
    {
        if ($id === false)
        {
            return FALSE;
        }
        else
        {
            $cmdStr = "";
            if (0 < $id)
            {
                $q = getdb("!!Select * From bet8 where id={$id} AND IFNULL(state,0)=0 and (rame=0 or rame=2)");
            }
            else
            {
                $q = getdb("!!Select * From bet8 where IFNULL(state,0)=0 and (rame=0 or rame=2)");
            }
            $this->m_lotto_id = 0;
            while (!$q->eof)
            {
                $this->m_lotto_id = $q->f['lotto_id'];
                if ($this->bettotal_by_lotto_id)
                {
                    $this->bettotal_6h = "bettotal_6h_" . $this->m_lotto_id;
                }
                else
                {
                    $this->bettotal_6h = "bettotal_6h";
                }
                if (empty($this->g_bettype_ab[intval($q->f['bettype'])][0]))
                {
                    $this->SetBet8Total($q, $q->f['bettype'], $q->f['bettype']);
                }
                else
                {
                    $this->SetBet8Total($q, $this->g_bettype_ab[intval($q->f['bettype'])][0], $this->g_bettype_ab[intval($q->f['bettype'])][1]);
                }
                getdb("Update bet8 Set state=1 Where id=" . $q->f['id'] . " LIMIT 1");
                $q->next();
            }
        }
    }

    public function SetBet8Total ($sqlQ, $btype1, $btype2)
    {
        $lotto_id = $sqlQ->f['lotto_id'];
        $bettype = $sqlQ->f['bettype'];
        $numbers = $sqlQ->f['numbers'];
        $rate = $sqlQ->f['rate'];
        $rame = $sqlQ->f['rame'] == 2 ? 0 - 1 : 1;
        $back = $sqlQ->f['back'];
        $amount = $sqlQ->f['amount'] * $rame;
        $is2526 = false;
        $u_id = "0";
        $back = $amount * $back / 100;
        $nums = array();
        if ($btype1 == 25 || $btype1 == 26 || $btype1 == 1003)
        {
            $is2526 = true;
            $p = explode(" ", $numbers);
            array_push($nums, $p[0] . "," . $p[1]);
        }
        else if ($btype1 == 19)
        {
            $p = explode("|", $numbers);
            array_push($nums, $p[0]);
        }
        else
        {
            array_push($nums, $numbers);
        }
        $cmdStr = "";
        $amount2 = 0;
        $amount3 = round($amount * $rate, 0);
        $amount4 = 0;
        if (!$is2526)
        {
            $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[0]}'");
            $amount4 = round($amount * $rate, 0);
            if ($sqlBetTotal->eof)
            {
                $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id, user_id ,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values (" . $lotto_id . ",0," . $btype1 . "," . "'" . $nums[0] . "',1," . ( $amount * ( 0 - 1 ) ) . "," . ( $amount * ( 0 - 1 ) ) . "," . ( $amount4 * ( 0 - 1 ) ) . "," . ( $amount4 * ( 0 - 1 ) ) . ",0,{$back},0,0,0";
                $k = 1;
                while ($k <= 4)
                {
                    $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                    ++$k;
                }
                $cmdStr .= "," . systime();
                $cmdStr .= ")";
            }
            else
            {
                $cmdStr = "Update {$this->bettotal_6h} Set " . "totalnum = totalnum +" . $rame . ",amount1 = amount1 -" . $amount . ",amount2 = amount2 -" . $amount . ",amount3 = amount3 -" . $amount4 . ",amount4 = amount4 -" . $amount4 . ",back2 = back2 +" . $back . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=0" . " and bettype=" . $btype1 . " and numbers='" . $nums[0] . "' LIMIT 1";
            }
            getdb($cmdStr);
        }
        else
        {
            $j = 0;
            while ($j < count($nums))
            {
                $sqlBetTotal = getdb("!!Select * From {$this->bettotal_6h} Where lotto_id={$lotto_id} and user_id={$u_id} and bettype={$btype1} and numbers='{$nums[$j]}'");
                if ($sqlBetTotal->eof)
                {
                    $amount4 = round($amount * $rate, 0);
                    $cmdStr = "Insert Into {$this->bettotal_6h} " . "(lotto_id, user_id ,bettype,numbers," . "totalnum,amount1,amount2,amount3,amount4,back1,back2,back3,rate1,rate2," . "totalnuma,amount1a,amount2a,amount3a,amount4a,back1a,back2a,back3a,rate1a,rate2a," . "totalnumb,amount1b,amount2b,amount3b,amount4b,back1b,back2b,back3b,rate1b,rate2b," . "totalnumc,amount1c,amount2c,amount3c,amount4c,back1c,back2c,back3c,rate1c,rate2c," . "totalnumd,amount1d,amount2d,amount3d,amount4d,back1d,back2d,back3d,rate1d,rate2d" . ",updatetime" . ") Values (" . $lotto_id . ",0," . $btype1 . "," . "'" . $nums[$j] . "',1," . ( $amount * ( 0 - 1 ) ) . "," . ( $amount * ( 0 - 1 ) ) . "," . ( $amount4 * ( 0 - 1 ) ) . "," . ( $amount4 * ( 0 - 1 ) ) . ",0,{$back},0,0,0";
                    $k = 1;
                    while ($k <= 4)
                    {
                        $cmdStr .= ",0,0,0,0,0,0,0,0,0,0";
                        ++$k;
                    }
                    $cmdStr .= "," . systime();
                    $cmdStr .= ")";
                }
                else
                {
                    $amount4 = round($amount * $rate, 0);
                    $cmdStr = "Update {$this->bettotal_6h} Set " . "totalnum = totalnum +" . $rame . ",amount1 = amount1 -" . $amount . ",amount2 = amount2 -" . $amount . ",amount3 = amount3 -" . $amount4 . ",amount4 = amount4 -" . $amount4 . ",back2 = back2 +" . $back . ",updatetime =" . systime() . " Where lotto_id=" . $lotto_id . " and user_id=0" . " and bettype=" . $btype1 . " and numbers='" . $nums[$j] . "' LIMIT 1";
                }
                getdb($cmdStr);
                ++$j;
            }
        }
    }

    public function ReBetTotalByLottoId ($lotto_id)
    {
        $bypass = "7,8,9,10,11,12,27,29,32,33,36,39,40,41,42,43,44,45";
        if ($this->bettotal_by_lotto_id)
        {
            $this->bettotal_6h = "bettotal_6h_" . $lotto_id;
        }
        else
        {
            $this->bettotal_6h = "bettotal_6h";
        }
        getdb("Update bet Set state=0,rame=0,sessid=null where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("Update bet2 Set state=0,rame=0,sessid=null where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("Update bet8 Set state=0,rame=0 where lotto_id={$lotto_id} and (rame=0 or rame=9)");
        getdb("delete from {$this->bettotal_6h} where lotto_id = {$lotto_id}");
        $this->autoport = false;
        $this->betmin = floatval($this->GetConfig("betmin"));
        $rs = getdb("!!Select * From bet2 Where IFNULL(state,0)=0 and lotto_id = {$lotto_id} and rame=0 and bettype not in ({$bypass}) order by acl DESC ,id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            if (empty($this->g_bettype_ab[intval($rs->f['bettype'])][0]))
            {
                $this->SetBet2Total($rs, $rs->f['bettype'], $rs->f['bettype']);
            }
            else
            {
                $this->SetBet2Total($rs, $this->g_bettype_ab[intval($rs->f['bettype'])][0], $this->g_bettype_ab[intval($rs->f['bettype'])][1]);
            }
            $cmdStr = "Update bet2 Set state=1,sessid='1' Where id=" . $rs->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $rs->next();
        }
        $rs = getdb("!!Select * From bet Where IFNULL(state,0)=0 and lotto_id = {$lotto_id} and bettype not in ({$bypass}) and rame=0 order by id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            if (empty($this->g_bettype_ab[intval($rs->f['bettype'])][0]))
            {
                $this->SetBetTotal($rs, $rs->f['bettype'], $rs->f['bettype']);
            }
            else
            {
                $old = $this->autoport;
                $this->autoport = false;
                $this->SetBetTotal($rs, $rs->f['bettype'], $rs->f['bettype']);
                $this->autoport = $old;
                $this->SetBetTotal($rs, $this->g_bettype_ab[intval($rs->f['bettype'])][0], $this->g_bettype_ab[intval($rs->f['bettype'])][1]);
            }
            $cmdStr = "Update bet Set state=1,sessid='1' Where id=" . $rs->f['id'] . " LIMIT 1";
            getdb($cmdStr);
            $rs->next();
        }
        $rs = getdb("!!Select * From bet8 where IFNULL(state,0)=0 and lotto_id = {$lotto_id} and bettype not in ({$bypass}) and rame=0 order by id");
        $this->m_lotto_id = 0;
        while (!$rs->eof)
        {
            $this->m_lotto_id = $rs->f['lotto_id'];
            if (empty($this->g_bettype_ab[intval($rs->f['bettype'])][0]))
            {
                $this->SetBet8Total($rs, $rs->f['bettype'], $rs->f['bettype']);
            }
            else
            {
                $this->SetBet8Total($rs, $this->g_bettype_ab[intval($rs->f['bettype'])][0], $this->g_bettype_ab[intval($rs->f['bettype'])][1]);
            }
            getdb("Update bet8 Set state=1 Where id=" . $rs->f['id'] . " LIMIT 1");
            $rs->next();
        }
        $rs = getdb("!!SELECT MAX(createtime) FROM bet WHERE lotto_id=" . $lotto_id);
        getdb("update bet2 set createtime = " . $rs->f[0] . " where lotto_id=" . $lotto_id . " and betmode=1");
    }

    public function LOG ($str)
    {
        $add_str = $str . "\n";
        if (!( $fp = fopen("lotto_6h_log.txt", "a") ))
        {
            echo "open error";
        }
        else
        {
            fwrite($fp, $add_str);
            fpassthru($fp);
        }
    }

}

?>